/**
  * @authors Tyler Williams, Cameron Stewart, Fatimah Alowa, Marlon Boothe, Lauren Walker
  * @version group2 project 2 part 2
  * @since 2012-09-15
  */

#include "common.h"

/**
* This method is resonsible for outputting the lines of the file as well as
* the line numbers, and calling the print_page_header when the line_count exceeds the max
* permitted per page
*
*@param line                       character array
*@param source_name_to_print       character array
*@param date_to_print              character array
*
*@return void
*/

void print_line(char line[], char source_name_to_print[], char date_to_print[]){
	char save_char[1] ={'\0'};
	char *save_char_ptr = NULL;
	static int line_count = MAX_LINES_PER_PAGE;
	line_count=line_count+1;
	
	if(line_count > MAX_LINES_PER_PAGE){
		print_page_header(source_name_to_print, date_to_print);
		line_count=1;
	}
	if(sizeof(line)/sizeof(char *) > MAX_PRINT_LINE_LENGTH){
		save_char_ptr = &line[sizeof(line)/sizeof(char *)-1];
	}
	if(save_char_ptr != NULL){
		strcpy(save_char, save_char_ptr);
		save_char_ptr = '\0';
	}
	printf("%s",line);
	if(save_char_ptr != NULL){
		save_char_ptr = save_char;
	}
}

/**
 *Method prints a header at the top of every new page
 *Called in print_line when there is the max capacity of lines for a page
 *
 *@param source_name    character array
 *@param date           character array
 *
 *@return void
 */

static void print_page_header(char source_name[], char date[]){ 
	static int page_number = 0;
	printf("%c",FORM_FEED_CHAR);
	page_number=page_number+1;
	printf("Page\t%d  %s %s \n",page_number, source_name, date);
}
